package com.lzx.adx.report.support.strategy.v3.media;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.lzs.adx.commmon.utils.JsonUtils;
import com.lzx.adx.report.annnotions.MediaType;
import com.lzx.adx.report.constant.MediaChannelConstant;
import com.lzx.adx.report.dto.*;
import com.lzx.adx.report.model.tencent.request.GTdCallBackRequest;
import com.lzx.adx.report.model.tencent.request.callback.Params;
import com.lzx.adx.report.model.tencent.request.callback.UserInfo;
import com.lzx.adx.report.support.strategy.v2.media.AbstractCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 广点通
 */
@Component
@Slf4j
@MediaType(MediaChannelConstant.GDT)
public class GdtCallBackChannel extends AbstractChannel {
    @Override
    public ResponseEntity callback(ReportDTO reportDTO, OcpxAccountDTO accountDTO, String callType, String ts) {
        boolean base = base(reportDTO, accountDTO, callType, ts);
        if(!base){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, CallTypeMappingDTO> callTypeMap = getCallTypeMap(accountDTO);
        CallTypeMappingDTO callTypeMappingDTO = callTypeMap.get(callType);
        String eventType=callTypeMappingDTO.getMediaReturnType();


        //2.回调渠道，调用腾讯回传接口
        Long actionTime=Long.valueOf(ts) / 1000;
        UserInfo userInfo = new UserInfo();
        userInfo.setHash_oaid(reportDTO.getOaidSum());
        userInfo.setHash_imei(reportDTO.getImeiSum());
        userInfo.setHash_android_id(reportDTO.getHash_android_id());
        Params params1 = new Params();
        params1.setAction_time(actionTime);
        params1.setAction_type(eventType);
        params1.setOuter_action_id(reportDTO.getBizRequestId());
        params1.setUser_id(userInfo);
        params1.setAction_param(new HashMap<>());
        GTdCallBackRequest gTdCallBackRequest = new GTdCallBackRequest();
        gTdCallBackRequest.setActions(Lists.newArrayList(params1));
        String paramsBody = JsonUtils.toJsonString(gTdCallBackRequest);
        log.info("账号为{} 请求流水号{} 广点通回调的请求的参数为{}", reportDTO.getDspId(), reportDTO.getBizRequestId(), paramsBody);
        String url = URLUtil.decode(reportDTO.getCallback());
        Map<String,String> headMap=new HashMap<>();
        headMap.put("access-token",accountDTO.getMediaKey());
        headMap.put("timestamp",String.valueOf(actionTime));
        headMap.put("nonce", IdUtil.fastSimpleUUID());
        HttpRequest body = HttpUtil.createPost(url).headerMap(headMap,true).body(paramsBody);
        String result = body.execute().body();
        log.info("账号为{} 请求流水号{} 上报给广点通转换响应的结果为{}", reportDTO.getDspId(), reportDTO.getBizRequestId(), result);

        accountDTO.setFinalURL(url);
        accountDTO.setBody(result);
        return ResponseEntity.ok(new GtdResult<>());
    }
}
